const reg = (() => {
    //   $.validator.addMethod("phone", function(value, element) {
    //     return this.optional(element) || /^1[3-9]\d{9}$/.test(value);
    //   });

    $.validator.addMethod("checkname", function (value, element) {
        return /^[a-z]+\w+/i.test(value);
    });



    axios.defaults.baseURL = 'http://60.204.218.206:8088';
    //  请求拦截器 interceptors  
    axios.interceptors.request.use(function (config) {
        // 在发送请求之前做些什么，携带token

        return config;
    }),
        // 响应拦截器
        axios.interceptors.response.use(function (config) {
            // 对响应数据做点什么
            return config;
        });


    const validate = () => {
        $("form").validate({
            rules: {
                name: {
                    required: true,  //required,rangelength等这些规则在jquery中有规定
                    rangelength: [6, 18],
                    checkname: true
                },
                pwd: {
                    required: true,

                },
                pwd2: {
                    required: true,
                    equalTo: "#pwd"   //根据<input name="pwd" id="pwd">  id="pwd"
                }
            },
            messages: {
                name: {
                    required: "请输入用户名",
                    rangelength: "用户名长度为{0}-{1}位",
                    checkname: "用户名必须以字母开头"
                },
                pwd: {
                    required: "请输入密码"
                },
                pwd2: {
                    required: "请输入确认密码",
                    equalTo: "两次密码不一致"
                }

            },
            submitHandler() {
                // //URLSearchParams 支持 post上application/x-www-form-urlencoed
                let data = new URLSearchParams()
                data.append("name", $('[name="name"]').val())
                data.append("pwd", $('[name="pwd"]').val())
                data.append("sex", $('[name="sex"]').val())

                axios({
                    url: "/account/reg",
                    method: "post",
                    data: data
                }).then(function (res) {
                    console.log(res);
                })

                
                return false
            }


        }

        )
    }
    return {
        validate
    }
})()

$(function () {
    reg.validate()
})